/**
 * 数据过滤工具
 */

// 模型字段白名单配置
const whiteList = {
    Article: ['title', 'content', 'categoryId', 'status'],
    User: ['username', 'password', 'email', 'nickname', 'avatar', 'sex', 'company', 'introduce', 'role'],
    Category: ['name', 'parentId', 'rank', 'status'],
    Setting: ['name', 'copyright']
};

/**
 * 通用数据过滤方法
 * @param {Object} body - 请求体数据
 * @param {String} modelName - 模型名称
 * @returns {Object} - 过滤后的数据
 */
exports.filterBody = (body, modelName) => {
    if (!whiteList[modelName]) {
        throw new Error(`未找到 ${modelName} 的白名单配置`);
    }

    const filteredData = {};
    whiteList[modelName].forEach(field => {
        if (body[field] !== undefined) {
            filteredData[field] = body[field];
        }
    });
    return filteredData;
};

// 分页参数处理
exports.getPaginationParams = (query) => {
    const currentPage = Math.abs(Number(query.currentPage)) || 1;
    const pageSize = Math.abs(Number(query.pageSize)) || 10;
    const offset = (currentPage - 1) * pageSize;
    
    return {
        currentPage,
        pageSize,
        offset
    };
}; 